// 简历网站增强交互功能

// 平滑滚动到指定区域
function scrollToSection(sectionId) {
    const element = document.getElementById(sectionId);
    if (element) {
        element.scrollIntoView({
            behavior: 'smooth',
            block: 'start'
        });
    }
}

// 返回顶部功能
function scrollToTop() {
    window.scrollTo({
        top: 0,
        behavior: 'smooth'
    });
}

// 监听滚动事件，添加导航高亮
window.addEventListener('scroll', function() {
    const sections = document.querySelectorAll('section[id]');
    const navLinks = document.querySelectorAll('a[href^="#"]');
    
    let current = '';
    sections.forEach(section => {
        const sectionTop = section.offsetTop;
        const sectionHeight = section.clientHeight;
        if (scrollY >= (sectionTop - 200)) {
            current = section.getAttribute('id');
        }
    });

    navLinks.forEach(link => {
        link.classList.remove('text-blue-600', 'font-bold');
        if (link.getAttribute('href') === '#' + current) {
            link.classList.add('text-blue-600', 'font-bold');
        }
    });
});

// 添加滚动显示动画
function animateOnScroll() {
    const elements = document.querySelectorAll('.card, .timeline-item');
    
    elements.forEach(element => {
        const elementTop = element.getBoundingClientRect().top;
        const elementVisible = 150;
        
        if (elementTop < window.innerHeight - elementVisible) {
            element.classList.add('animate-fadeInUp');
        }
    });
}

// 页面加载完成后执行
window.addEventListener('load', function() {
    animateOnScroll();
    
    // 添加加载完成动画
    document.body.classList.add('loaded');
});

// 滚动时触发动画
window.addEventListener('scroll', animateOnScroll);

// 技能进度条动画
function animateSkillBars() {
    const skillBars = document.querySelectorAll('.skill-progress');
    skillBars.forEach(bar => {
        const width = bar.getAttribute('data-width') || '100%';
        bar.style.width = '0%';
        setTimeout(() => {
            bar.style.width = width;
        }, 200);
    });
}

// 打印简历功能
function printResume() {
    window.print();
}

// 分享简历功能
function shareResume() {
    if (navigator.share) {
        navigator.share({
            title: '蔡然发 - Java开发工程师简历',
            text: '查看我的在线简历',
            url: window.location.href
        });
    } else {
        // 复制链接到剪贴板
        navigator.clipboard.writeText(window.location.href).then(() => {
            alert('链接已复制到剪贴板！');
        });
    }
}

// 下载简历为PDF（需要html2pdf库）
function downloadResume() {
    const element = document.getElementById('app');
    const opt = {
        margin: 1,
        filename: '蔡然发-Java开发工程师简历.pdf',
        image: { type: 'jpeg', quality: 0.98 },
        html2canvas: { scale: 2 },
        jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' }
    };
    
    // 注意：这需要引入html2pdf.js库
    if (window.html2pdf) {
        html2pdf().set(opt).from(element).save();
    } else {
        alert('PDF下载功能需要额外库支持');
    }
}

// 添加键盘快捷键
document.addEventListener('keydown', function(e) {
    // Ctrl/Cmd + P 打印
    if ((e.ctrlKey || e.metaKey) && e.key === 'p') {
        e.preventDefault();
        printResume();
    }
    
    // Ctrl/Cmd + Home 返回顶部
    if ((e.ctrlKey || e.metaKey) && e.key === 'Home') {
        e.preventDefault();
        scrollToTop();
    }
});

// 移动端触摸优化
if ('ontouchstart' in window) {
    document.body.classList.add('touch-device');
    
    // 添加触摸反馈
    const touchElements = document.querySelectorAll('.card, .btn');
    touchElements.forEach(element => {
        element.addEventListener('touchstart', function() {
            this.style.transform = 'scale(0.98)';
        });
        
        element.addEventListener('touchend', function() {
            this.style.transform = '';
        });
    });
}

// 性能优化：图片懒加载
if ('IntersectionObserver' in window) {
    const imageObserver = new IntersectionObserver((entries, observer) => {
        entries.forEach(entry => {
            if (entry.isIntersecting) {
                const img = entry.target;
                img.src = img.dataset.src;
                img.classList.remove('lazy');
                observer.unobserve(img);
            }
        });
    });

    document.querySelectorAll('img[data-src]').forEach(img => {
        imageObserver.observe(img);
    });
}

// 暗黑模式切换（可选功能）
function toggleDarkMode() {
    document.body.classList.toggle('dark-mode');
    localStorage.setItem('darkMode', document.body.classList.contains('dark-mode'));
}

// 初始化暗黑模式
if (localStorage.getItem('darkMode') === 'true') {
    document.body.classList.add('dark-mode');
}

// 导出功能供全局使用
window.resumeApp = {
    scrollToSection,
    scrollToTop,
    printResume,
    shareResume,
    downloadResume,
    toggleDarkMode
};